package ua.kpi_java_training5.alekseenko.src.module2;

/**
 * Author: Aleksey Alekseenko
 * Date: 04.06.13
 */
public class Victim extends Fish {
    public Victim() {
        this.eatable = true;
    }

    public Victim(int x, int y) {
        this.position.x = x;
        this.position.y = y;
        this.eatable = false;
    }

    @Override
    public void move(int step) {
        boolean untilFishIsInAquarium;
        do {
            untilFishIsInAquarium = false;
            switch (DirectionMove.selectDirection()) {
                case LEFT:
                    if (this.position.x - step >= 0) {
                        this.position.x -= step;
                    } else {
                        untilFishIsInAquarium = true;
                    }
                    break;

                case RIGHT:
                    if (this.position.x + step <= aquarium.getLength()) {
                        this.position.x += step;
                    } else {
                        untilFishIsInAquarium = true;
                    }
                    break;

                case DOWN:
                    if (this.position.y - step >= 0) {
                        this.position.y -= step;
                    } else {
                        untilFishIsInAquarium = true;
                    }
                    break;

                case UP:
                    if (this.position.y + step <= aquarium.getHeight()) {
                        this.position.y += step;
                    } else {
                        untilFishIsInAquarium = true;
                    }
                    break;
            }
        } while (untilFishIsInAquarium);
        this.age += 1;
        birthNewVictim();
    }

    /**
     * Births new Victim after 5 moves
     */
    private void birthNewVictim() {
        if (this.age % 5 == 0) {
            Victim victim = new Victim();
            victim.position = new Position((int) (Math.random() * aquarium.getLength()), (int) (Math.random() * aquarium.getHeight()));
            fishFactory.victims.add(victim);
            Statistic.spawnedVictims++;
        }
    }

}
